/*******************************************************************************
 * Copyright (c) 2004, 2009 Blue Bamboo International Inc. 
 * All rights reserved.
 *
 * Blue Bamboo PROPRIETARY/CONFIDENTIAL.
 *******************************************************************************/

package com.bambamboo.st.util;

import java.util.Collection;

public class StringUtil {

	public static String ltrim(String src, char ch, int nLen) {
		if (src == null || src.length() <= nLen)
			return src;

		char[] czSrc = src.toCharArray();
		int i, j;
		int n = czSrc.length;
		for (i = 0; (n - i) > nLen; i++) {
			if (czSrc[i] != ch)
				break;
		}

		char[] czRet = new char[n - i];
		for (j = 0; i < czSrc.length; i++, j++)
			czRet[j] = czSrc[i];

		return new String(czRet);
	}

	public static String rtrim(String src, char ch, int nLen) {
		if (src == null || src.length() <= nLen)
			return src;

		char[] czSrc = src.toCharArray();
		int i, j;
		int n = czSrc.length;
		for (i = n - 1; i >= nLen; i--) {
			if (czSrc[i] != ch)
				break;
		}

		char[] czRet = new char[i + 1];
		for (j = i; i >= 0; i--, j--)
			czRet[j] = czSrc[i];

		return new String(czRet);
	}

	/**
	 * @param s
	 * @return true if <code>s</code> is <code>null</code> or <b>empty</b>
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim()))
			return true;
		return false;
	}

	public static String fillChar(String sSource, char ch, int nLen, boolean bLeft) {
		int nSrcLen = sSource.length();

		if (nSrcLen <= nLen) {
			StringBuffer buffer = new StringBuffer();
			if (bLeft) {
				for (int i = 0; i < (nLen - nSrcLen); i++) {
					buffer.append(ch);
				}
				buffer.append(sSource);
			}
			else {
				buffer.append(sSource);
				for (int i = 0; i < (nLen - nSrcLen); i++)
					buffer.append(ch);
			}
			return (buffer.toString());
		}
		return sSource;
	}

	public static String collectionToString(Collection coll, char sep) {
		StringBuffer buffer = new StringBuffer();
		if (coll != null) {
			for (Object obj : coll) {
				if (buffer.length() > 0) {
					buffer.append(sep);
				}
				buffer.append(obj);
			}
		}
		return buffer.toString();
	}

	public static String arrayToString(Object[] objs) {
		StringBuffer buffer = new StringBuffer();
		if (objs != null) {
			for (int i = 0; i < objs.length; i++) {
				if (buffer.length() > 0)
					buffer.append("\n");
				buffer.append(objs[i]);
			}
		}
		return buffer.toString();
	}

	/**
	 * Returns the extension of a given file full path or name. The extension is any text at the right side of the last
	 * '.'.
	 * 
	 * <p>
	 * If the file has no extension or is null returns an empty string.
	 * 
	 * @param file
	 * @return String
	 */
	public static String getFileExtension(String file) {
		if (file == null) {
			throw new IllegalArgumentException("null arg: file");
		}

		int dotIdx = file.lastIndexOf('.');
		String ext = "";
		if (dotIdx > 0 && dotIdx != (file.length() - 1)) {
			ext = file.substring(dotIdx + 1);
		}

		return ext;
	}

	public static String getFileName(String file) {
		if (file == null)
			return "";
		int sIdx = file.lastIndexOf('/');
		int bsIdx = file.lastIndexOf('\\');
		int idx = Math.max(sIdx, bsIdx);

		return file.substring(idx + 1);
	}
}
